"""
The Game Menu file.
$Id: menu.py,v 1.7 2004/12/17 09:57:50 krawczyk Exp $

Global Variables:
no_of_players -- The current number of players
"""

import pygame, random, math
from pygame.locals import *
import config, mixer, snowball, flocke

class Data:
    def __init__(self):
        MENU=config.menu
        self.scr        = config.scr #Screen surface
        self.scr_bg     = MENU["bg"] #BG colour
        self.font       = pygame.font.Font(None, 60)
        self.font_color = MENU["fg"]
        self.oldrect    = [] #List of old rectangles
        print "Loading title picture...",
        self.titelpic      = pygame.image.load(MENU["titelpicfile"]).convert_alpha()
        self.titelpicwidth = self.titelpic.get_width()
        print "OK"
        self.titelpic_xpos = (config.scrsize[0]-self.titelpicwidth)/2
        self.titelpic_ypos = 50
        self.titelpic.set_alpha(255,RLEACCEL)

def Init():
    global data
    print "Initialising Menu...",
    config.Status_top("Initialising Menu...")
    data = Data()
    print "Done!"
    
def _update_display():
    global data
    r = []
    r.append(data.scr.fill(data.scr_bg, data.oldrect))
    y = data.text_y
    for i in data.text:
        data.scr.blit(i, (data.text_x, y))
        y += 65
    sin = math.sin(data.omega/2.1)
    x = (sin*sin*sin+1)/2*(config.scrsize[0]-data.titelpicwidth)
    sin = math.sin(data.omega)    
    y = data.titelpic_ypos + sin*20
    t = data.scr.blit(data.titelpic, (x, y))
    data.oldrect = t
    r.append(t) # We need only to update the logo
    pygame.display.update(r)
    data.omega += 120.0/360/1000*config.fps

def _init():
    global data
    
    MENU=config.menu
    mixer.mixer.load(MENU["music"])
    mixer.mixer.play(-1) #Endlosschleife
    data.scr.fill(data.scr_bg)

def Playerline():
    return "F3 -- "+str(no_of_players)+" Players"

def main():
    global data, no_of_players
    _init()
    t = []
    t.append( data.font.render("F1  -- Play Game", 0, data.font_color) )
    t.append( data.font.render("F2  -- Toogle Full Screen", 0, data.font_color) )
    t.append( data.font.render(Playerline(), 0, data.font_color) )
    t.append( data.font.render("ESC -- Quit Game", 0, data.font_color) )
    data.text   = t
    data.text_y = 333
    data.text_x = 100
    data.omega  = 0
    data.oldrect = data.scr.get_rect()

    ende = False
    while not ende:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                k = event.key  
                if k == K_F1:
                    return False
                if k == K_F2:
                    pygame.display.toggle_fullscreen()
                if k == K_F3:
                    no_of_players += 1
                    if no_of_players > 2:
                        no_of_players = 0
                    t[2] = data.font.render(Playerline(), 0, data.font_color)
                    data.oldrect = data.scr.get_rect()
                elif k == K_ESCAPE:
                    return True
            elif event.type == QUIT:
                ende = True
                break
        _update_display()
        pygame.time.wait(1000/config.fps)

    del data
    return ende

no_of_players = 2
